<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>联系人列表</title>
  <style>
    table {
      border-collapse: collapse;
      width: 100%;
    }

    th,
    td {
      border: 1px solid black;
      padding: 8px;
      text-align: left;
    }

    th {
      background-color: #f2f2f2;
    }

    tr:nth-child(even) {
      background-color: #f9f9f9;
    }

    .id-column {
      font-weight: bold;
    }

    .modal {
      display: none;
      position: fixed;
      z-index: 1;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      overflow: auto;
      background-color: rgba(0, 0, 0, 0.4);
    }

    .modal-content {
      background-color: #fefefe;
      margin: 15% auto;
      padding: 20px;
      border: 1px solid #888;
      width: 50%;
    }

    /* 模态框外部遮罩层 */
    .modal {
      display: none;
      /* 默认隐藏 */
      position: fixed;
      /* 固定定位 */
      z-index: 1;
      /* 设置z-index以确保模态框显示在其他元素之上 */
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.4);
      /* 半透明黑色背景 */
    }

    /* 模态框内容区域 */
    .modal-content {
      background-color: #fefefe;
      /* 白色背景 */
      margin: 10% auto;
      /* 居中对齐 */
      padding: 20px;
      border: 1px solid #888;
      width: 80%;
      max-width: 600px;
    }

    /* 标题样式 */
    .modal-content h2 {
      margin-top: 0;
    }

    /* 表单样式 */
    .modal-content form {
      display: flex;
      flex-direction: column;
    }

    /* 表单字段样式 */
    .modal-content input[type=text],
    .modal-content input[type=email],
    .modal-content input[type=tel] {
      margin-bottom: 10px;
      padding: 10px;
      border: none;
      border-radius: 3px;
      box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
    }

    /* 按钮样式 */
    .modal-content button[type=submit] {
      margin-top: 10px;
      padding: 10px;
      background-color: #4CAF50;
      /* 绿色背景 */
      color: white;
      border: none;
      border-radius: 3px;
      cursor: pointer;
    }
  </style>
</head>

<body>
  <h1>联系人列表</h1>
  <table id="contacts-table">
    <thead>
      <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>邮箱</th>
        <th>电话</th>
        <th>创建时间</th>
        <th>更新时间</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <% contacts.forEach(contact=> { %>
        <tr>
          <td class="id-column">
            <%= contact.id %>
          </td>
          <td>
            <%= contact.name %>
          </td>
          <td>
            <%= contact.email %>
          </td>
          <td>
            <%= contact.phone %>
          </td>
          <td>
            <%= contact.createdAt.toLocaleString() %>
          </td>
          <td>
            <%= contact.updatedAt.toLocaleString() %>
          </td>
          <td>
            <button class="edit-button" data-id="<%= contact.id %>">修改</button>
            <button class="delete-button" data-id="<%= contact.id %>">删除</button>
          </td>
        </tr>
        <% }) %>
    </tbody>
  </table>

  <h2>添加联系人</h2>
  <form id="add-contact-form">
    <label for="name">姓名：</label>
    <input type="text" name="name" required>

    <label for="email">邮箱：</label>
    <input type="email" name="email" required>

    <label for="phone">电话：</label>
    <input type="tel" name="phone" required>

    <button type="submit">添加联系人</button>
  </form>

  <div id="edit-contact-modal" class="modal">
    <div class="modal-content">
      <h2>修改联系人</h2>
      <form id="edit-contact-form">
        <input type="hidden" name="id">

        <label for="name">姓名：</label>
        <input type="text" name="name" required>

        <label for="email">邮箱：</label>
        <input type="email" name="email" required>

        <label for="phone">电话：</label>
        <input type="tel" name="phone" required>

        <button type="submit">保存修改</button>
      </form>
    </div>
  </div>


  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
    $(function () {
      // 添加联系人
      $('#add-contact-form').submit(function (event) {
        event.preventDefault();
        const formData = $(this).serialize();
        $.ajax({
          url: '/contacts',
          method: 'POST',
          data: formData,
          success: function (data) {
            location.reload();
          },
          error: function () {
            alert('添加联系人失败');
          }
        });
      });

      // 编辑联系人
      $('#contacts-table').on('click', '.edit-button', function () {
        const contactId = $(this).data('id');
        $.ajax({
          url: `/contacts/${contactId}`,
          method: 'GET',
          success: function (data) {
            $('#edit-contact-form [name=id]').val(data.id);
            $('#edit-contact-form [name=name]').val(data.name);
            $('#edit-contact-form [name=email]').val(data.email);
            $('#edit-contact-form [name=phone]').val(data.phone);
            $('#edit-contact-modal').show(); // 显示模态框
          },
          error: function () {
            alert('获取联系人信息失败');
          }
        });
      });

      // 保存修改
      $('#edit-contact-form').submit(function (event) {
        event.preventDefault();
        const formData = $(this).serialize();
        const contactId = $('#edit-contact-form [name=id]').val();
        $.ajax({
          url: `/contacts/${contactId}`,
          method: 'PUT',
          data: formData,
          success: function (data) {
            console.log('联系人修改成功');
            location.reload();
          },
          error: function () {
            alert('修改联系人失败');
          }
        });
      });


      // 关闭模态框
      $('#edit-contact-modal').on('click', function (event) {
        if (event.target === this) {
          $(this).hide();
        }
      });

      // 删除联系人
      $('#contacts-table').on('click', '.delete-button', function () {
        const contactId = $(this).data('id');
        if (confirm('确定要删除该联系人吗？')) {
          $.ajax({
            url: `/contacts/${contactId}`,
            method: 'DELETE',
            success: function (data) {
              console.log('联系人删除成功');
              location.reload();
            },
            error: function () {
              // alert('删除联系人失败');
              location.reload();
            }
          });
        }
      });

    });
  </script>
</body>

</html>